package com.erp.collect.controller;

import com.erp.collect.service.IErpKakaCollectProductService;
import com.ruoyi.collect.api.domain.bo.ErpKakaCollectProductBo;
import com.ruoyi.collect.api.domain.vo.ErpKakaCollectProductVo;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 采集商品
 *
 * @author erp
 * @date 2024-05-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/collect/product")
public class ErpKakaCollectProductController extends BaseController {

    private final IErpKakaCollectProductService iErpKakaCollectProductService;

    /**
     * 查询采集商品列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpKakaCollectProductVo> list(ErpKakaCollectProductBo bo, PageQuery pageQuery) {
        return iErpKakaCollectProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取采集商品详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpKakaCollectProductVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpKakaCollectProductService.queryById(id));
    }

    /**
     * 新增采集商品
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpKakaCollectProductBo bo) {
        return toAjax(iErpKakaCollectProductService.insertByBo(bo));
    }

    /**
     * 修改采集商品
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpKakaCollectProductBo bo) {
        return toAjax(iErpKakaCollectProductService.updateByBo(bo));
    }

    /**
     * 删除采集商品
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpKakaCollectProductService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 删除采集商品
     *
     * @param id 主键串
     */
    @DeleteMapping("/removeById/{id}")
    public R<Void> removeById(@NotEmpty(message = "主键不能为空")
                          @PathVariable String id) {
        List<Long> ids = new ArrayList<>();
        ids.add(Long.parseLong(id));
        return toAjax(iErpKakaCollectProductService.deleteWithValidByIds(ids, true));
    }

    /**
     * 获取采集商品所有图片信息
     *
     * @param id 主键
     */
    @GetMapping("/imgMap/{id}")
    public Map<String, Object> getImgUrlById(@PathVariable Long id) {
        return iErpKakaCollectProductService.queryImgUrlById(id);
    }

    /**
     * 修改保存采集商品所有图片信息
     *
     * @param map
     */
    @RepeatSubmit()
    @PostMapping("/update/img")
    public R<Void> getImgUrlById(@RequestBody Map<String, Object> map) {
        iErpKakaCollectProductService.updateProductImgs(map);
        return R.ok();
    }

    /**
     * 获取采集商品所有图片信息
     *
     * @param id 主键
     */
    @GetMapping("/imgMap/next/{id}/{next}")
    public Map<String, Object> queryImgUrlByIdAndNext(@PathVariable Long id, @PathVariable Integer next) {
        return iErpKakaCollectProductService.queryImgUrlByIdAndNext(id, next);
    }


    /**
     * 生成白底图
     *
     * @return
     */
    @GetMapping("/generateWhite/{imgUrl}")
    public R<Object> generateWhite(@PathVariable("imgUrl") String imgUrl) {
        // 远程调用，特殊处理/问题
        String url = iErpKakaCollectProductService.generateWhite(imgUrl.replaceAll("@@@", "/"));
        return url.startsWith("http") ? R.ok(url) : R.fail(url);
    }

    /**
     * 生成营销图
     *
     * @return
     */
    @GetMapping("/generateMarketing/{imgUrl}")
    public R<Object> generateMarketing(@PathVariable("imgUrl") String imgUrl) {
        // 远程调用，特殊处理/问题
        String url = iErpKakaCollectProductService.generateMarketing(imgUrl.replaceAll("@@@", "/"));
        return url.startsWith("http") ? R.ok(url) : R.fail(url);
    }

}
